package com.doumuxie.linstener;


import com.doumuxie.dto.ProcessDto;
import com.doumuxie.frame.ChildFrame;
import com.doumuxie.handler.ProcessHandler;
import com.doumuxie.util.SysCode;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @author doumuxie https://gitee.com/doumuxie/
 * @version 1.0
 * @date 2020/9/26 9:00
 * @description 子窗口事件
 **/
public class ChildListener implements ActionListener {

    private ChildFrame frame;

    private int id = 0;

    public ChildListener(ChildFrame frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();
        if (source == frame.getCreateBtn()) {
            // 创建线程事件
            Random random = new Random();
            // 创建线程
            ProcessDto process = new ProcessDto(frame);
            process.setId(++id + "");
            process.setLevel(random.nextInt(20));
            process.setState(SysCode.PROCESS_STATE.NEW);
            process.setRun(false);
            process.setName("模拟进程_" + process.getId());
            process.setTimeSliceCount(random.nextInt(5) + 1);
            System.out.println(process.toString());
            // 加入list
            ProcessHandler.add(process);
            Thread thread = process.getThread();
            System.err.println("当前线程状态：" + thread.getState());
        } else if (source == frame.getBlockMenItem()) {
            // 阻塞
            ProcessDto process = getProcessByTableSelect();
            if (process == null) {
                return;
            }
            process.setState(SysCode.PROCESS_STATE.BLOCK);
            // 执行其他进程
            ProcessHandler.nextProcess(process.getId());
        } else if (source == frame.getRunMenItem()) {
            // 运行
            ProcessDto process = getProcessByTableSelect();
            if (process == null) {
                return;
            }
            if (SysCode.PROCESS_STATE.DEATH.equals(process.getState())) {
                System.err.println("==========线程已经消亡==========");
                return;
            }
            Thread thread = process.getThread();
            if (Thread.State.NEW.equals(thread.getState())) {
                thread.start();
            }
            //上一个进程
            if (ProcessHandler.runningProcessId != null && !process.getId().equals(ProcessHandler.runningProcessId)) {
                ProcessDto processDto = ProcessHandler.get(ProcessHandler.runningProcessId);
                if (SysCode.PROCESS_STATE.RUN.equals(processDto.getState())) {
                    processDto.setState(SysCode.PROCESS_STATE.BLOCK);
                    processDto.setRun(false);
                }
            }
            ProcessHandler.runningProcessId = process.getId();
            process.setRun(true);
            process.setState(SysCode.PROCESS_STATE.RUN);
        } else if (source == frame.getOpenMenItem()) {
            // 阻塞唤醒 到 就绪状态
            // 阻塞
            ProcessDto process = getProcessByTableSelect();
            if (process == null) {
                return;
            }
            process.setState(SysCode.PROCESS_STATE.NEW);
        } else if (source == frame.getDeleteMenItem()) {
            // 删除
            ProcessDto process = getProcessByTableSelect();
            if (process == null) {
                return;
            }
            ProcessHandler.remove(process);
        }
        // 更新表格数据
        ProcessHandler.initRowData(frame);
    }

    /**
     * 获取列表选择的数据
     *
     * @return
     */
    private ProcessDto getProcessByTableSelect() {
        int selectedRow = frame.getTableOne().getSelectedRow();
        if (selectedRow < 0) {
            return null;
        }
        return ProcessHandler.get(selectedRow);
    }

}
